package com.changgou.filter;

import com.changgou.util.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.security.jwt.Jwt;
import org.springframework.security.jwt.JwtHelper;
import org.springframework.security.jwt.crypto.sign.RsaVerifier;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author ljh
 * @version 1.0
 * @date 2021/1/2 15:22
 * @description 标题
 * @package com.changgou.filter
 */
@Component
public class AuthorizeFilter implements GlobalFilter, Ordered {
    private static final String AUTHORIZE_TOKEN_HEADER = "Authorization";
    //业务逻辑处理
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

        //1.先获取请求对象
        ServerHttpRequest request = exchange.getRequest();
        //2.再获取响应对象
        ServerHttpResponse response = exchange.getResponse();
        //3.判断如果是登录的路径 放行
        String path = request.getURI().getPath();
        //存储需要开放的请求路径 具体可以设置在某一个properties文件中进行配置 白名单
        if(path.startsWith("/front/user/user/login")){
           return  chain.filter(exchange);
        }
        //4.先从请求参数中获取令牌 如果没有获取到
        String token = request.getQueryParams().getFirst(AUTHORIZE_TOKEN_HEADER);
        if(StringUtils.isEmpty(token)){
            //5.再从请求头中获取令牌  如果没有获取到
            token = request.getHeaders().getFirst(AUTHORIZE_TOKEN_HEADER);
        }
        if(StringUtils.isEmpty(token)){
            //6.再从cookie中获取令牌 如果没有获取到  拦截 返回 401
            HttpCookie cookie = request.getCookies().getFirst(AUTHORIZE_TOKEN_HEADER);
            if(cookie!=null){
                token = cookie.getValue();//令牌的数据
            }
        }
        if(StringUtils.isEmpty(token)){//结束
            //直接返回401
            //重定向到登录的页面  1.设置重定向到的地址 2.设置http状态码的行为 303 302
            //response.getHeaders().set("Location","http://localhost:9001/oauth/login?url="+request.getURI().toString());//url
            response.getHeaders().set("Location","http://passport.changgou.com/oauth/login?url="+request.getURI().toString());//url
            response.setStatusCode(HttpStatus.SEE_OTHER);
            return response.setComplete();
        }
        //7.如果能获取到 还要解析令牌 解析不成功  拦截 返回 401
       /* try {
           JwtUtil.parseJWT(token);
        } catch (Exception e) {
            e.printStackTrace();
            response.setStatusCode(HttpStatus.UNAUTHORIZED);
            return response.setComplete();
        }*/

        String claims = null;
        try {
            Jwt jwt = JwtHelper.decodeAndVerify(token, new RsaVerifier(JwtUtil.publickey));
            claims = jwt.getClaims();
        } catch (Exception e) {
            e.printStackTrace();
            //错误 直接返回
            response.getHeaders().set("Location","http://localhost:9001/oauth/login?url="+request.getURI().toString());//url
            response.setStatusCode(HttpStatus.SEE_OTHER);
            return response.setComplete();
        }
        //ej.askfdlaf.asdfafafa
        System.out.println(claims);
        //实现一个判断 是否是前台用户类型的登录判断。
        //获取到数据 放到头里面
        request.mutate().header(AUTHORIZE_TOKEN_HEADER,claims);


        //8.如果能解析出来 放行
        return  chain.filter(exchange);
    }

    @Override
    //数据值越低 过滤器的执行的优先级 越高
    public int getOrder() {
        return 0;
    }
}
